package cn.iocoder.yudao.framework.websocket.core.interceptor;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;


@Slf4j
public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {


	private Logger logger = LoggerFactory.getLogger(WebSocketHandshakeInterceptor.class);

    public static final String HTTP_REQUEST = "HTTP_REQUEST";

    private final int maxConnection = 100;
    public WebSocketHandshakeInterceptor(){

    }

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Map<String, Object> attributes) throws Exception {
		logger.info("开始连接websocket服务");
//        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
//        if (ObjectUtil.isNull(loginUser)){
//            return false;
//        }
        attributes.put(HTTP_REQUEST, request);
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception exception) {
		log.info("连接成功");
	}

}
